<?php
/**
 * 医院信息管理控制器
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/23
 * Time: 20:33
 */

namespace Admin\Controller;
use Admin\Model\AerainfoModel;
use Admin\Model\AppversioninfoModel;
use Admin\Model\BaseinfoVersionModel;
use Admin\Model\CityModel;
use Admin\Model\DistrictModel;
use Admin\Model\InspectiondetailinfoModel;
use Base\Controller\CurdController;

class InspectiondetailinfoController extends CurdController
{
    public function __construct()
    {
        parent::__construct(true, true, array(
                'input_name' => 'coverFile',//文本框name
                'save_path' => 'hospitalcover',//保存路径
                'data_field' => 'cover'//数据库字段
            )
        );
    }
    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['refvalue'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['attachvalue']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if($_REQUEST['medicalid']){
            $where['medicalid']=$_REQUEST['medicalid'];
        }
        if($_REQUEST['itemid']){
            $where['itemid']=$_REQUEST['itemid'];
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);

        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }

    /**
     * ajax提交修改化验单化验项的值
     */
    public function edits()
    {
        $where['id']=$_REQUEST['id'];
        $where['refvalue']=$_REQUEST['refvalue'];
        $where['attachvalue']=$_REQUEST['attachvalue'];
        $where['threshold']=$_REQUEST['threshold'];
        $where['boundary_value']=$_REQUEST['boundary_value'];
        $results =$this->model->save($where);
        if ($results) {
            $this->editversion($_REQUEST['id']);
            $where['status']=1;
            $where['code']=100;
            $this->ajaxReturn($where);
        } else {
            $where['status']=0;
            $where['code']=10;
            $this->ajaxReturn($where);
        }
    }
    public function defaultselect()
    {
        $where['id']=$_REQUEST['id'];
        $where['defaultselect']=$_REQUEST['defaultselect'];
        $result=$this->model->save($where);
        if($result){
            $this->editversion($_REQUEST['id']);
            $data['status']=1;
            $data['msg']='修改成功';
        }else{
            $data['status']=0;
            $data['msg']='未修改';
        }
        $this->ajaxReturn($data);
    }
    /**
     * 修改版本号
     */
    public  function editversion($id){
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $InspectiondetailinfoModel= new  InspectiondetailinfoModel();
        $wheres['name']='inspectiondetailinfo';
        $inspectiondetailinfo=$BaseinfoVersionModel->where($wheres)->find();
        $datas['id']=$inspectiondetailinfo['id'];
        $datas['version'] = $inspectiondetailinfo['version']+1;
        $res= $BaseinfoVersionModel->save($datas);
        if($res){
            $infos=$BaseinfoVersionModel->find($inspectiondetailinfo['id']);
            $dataes['id']=$id;
            $dataes['version']=$infos['version'];
            $dataes['status']=1;
            $InspectiondetailinfoModel->save($dataes);
        }
    }
    protected function addAfter($data)
    {
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        return $data;
    }
}
